<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <label class="el-form-item-label" />
        <el-input
          v-model="query.sourceNo"
          clearable
          placeholder="入庫單號"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.remark"
          clearable
          placeholder="備註"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query['inProduct.nameCn']"
          clearable
          placeholder="出庫商品"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!-- 搜索 -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="1000px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="130px" style="height: 300px">
          <el-row>

            <el-col :span="12">
              <el-form-item label="仓库" prop="depotId">
                <el-select v-model="form.depotId" placeholder="請選擇倉庫" style="width: 220px" @change="getDepotBillIn">
                  <el-option
                    v-for="item in depot"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="入庫單號">
                <el-input
                  :value="form.sourceNo"
                  icon="el-icon-plus"
                  placeholder="请点击选择入库单"
                  suffix-icon="el-icon-search"
                  :disabled="form.depotId == null"
                  style="width: 220px"
                  @focus="showBillInDialog"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="12">
            <el-form-item label="調整商品">
              <div v-if="inProduct !== null">{{ inProduct.product.nameCn }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供應商">
              <div v-if="inProduct!== null">{{ inProduct.vendorName }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品调整前數量">
              <div>{{ form.adjQuantityBefore }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品调整數量" prop="adjQuantity">
              <el-input-number v-model.number="form.adjQuantity" style="width: 220px" @change="computeAfter" />
              <span style="color: #d9dadd">負數即為減少</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品调整后數量">
              <div>{{ form.adjQuantityAfter }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注">
              <el-input v-model="form.remark" style="width: 220px" />
            </el-form-item>
          </el-col>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="defaultNumber" label="单据号" />
        <el-table-column prop="sourceNo" label="入庫單號" />
        <el-table-column prop="depotId" label="仓库">
          <template slot-scope="scope">
            {{ scope.row.depot.name }}
          </template>
        </el-table-column>
        <el-table-column label="調整产品" width="300">
          <template slot-scope="scope">
            {{ scope.row.inProduct.nameCn }}
          </template>
        </el-table-column>
        <el-table-column prop="adjQuantity" label="調整数量" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column

          label="操作"
          width="150px"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="seeTransfer(scope.row)"
            >查看詳情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog
      title="選擇入庫單商品"
      :visible.sync="BillInProductVisible"
      width="800px"
    >
      <el-input
        v-model="productQuery.productName"
        clearable
        placeholder="商品名稱"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="toQuery"
      />
      <el-input
        v-model="productQuery.sku"
        clearable
        placeholder="商品編號"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="toQuery"
      />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <el-table
        ref="BillInTable"
        :data="depotBillIn"
        size="mini"
        style="width: 100%;"
        stripe
        height="400px"
        @selection-change="getSelectionDepotBillIn"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="depotBillInCode" label="入庫單號">
          <template slot-scope="scope">
            {{ scope.row.depotBillInCode }}
          </template>
        </el-table-column>
        <el-table-column prop="vendorName" label="供應商">
          <template slot-scope="scope">
            {{ scope.row.vendorName }}
          </template>
        </el-table-column>
        <el-table-column prop="sku" label="商品編號">
          <template slot-scope="scope">
            {{ scope.row.product.sku }}
          </template>
        </el-table-column>
        <el-table-column prop="nameCn" label="商品名称" width="300px">
          <template slot-scope="scope">
            {{ scope.row.product.nameCn }}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size.sync="productQuery.size"
        :total="productQuery.total"
        :current-page.sync="productQuery.page"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      />
      <span slot="footer">
        <el-button @click="BillInProductVisible = false">取 消</el-button>
        <el-button
          type="primary"
          :disabled="selectionDepotBillInProduct.length !== 1"
          @click="BillInProduct"
        >确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="viewDetailVisible"
      title="查看調撥詳情"
      width="1000px"
    >
      <el-form v-if="seeForm.depot" size="small" label-width="130px" style="height: 300px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库:">
              {{ seeForm.depot.name }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入庫單號:">
              {{ seeForm.sourceNo }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="出庫商品名稱:">
              <div>{{ seeForm.inProduct.nameCn }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品调整前數量:">
              {{ seeForm.adjQuantityBefore }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品调整數量:">
              {{ seeForm.adjQuantity }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品调整后數量:">
              {{ seeForm.adjQuantityAfter }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注:">
              <div>{{ seeForm.remark }}</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import crudAdjustment from '@/api/wms/adjustment'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import depotBillIn from '@/api/wms/depotBillIn'
import { getAll } from '@/api/wms/depot'
import rrOperation from '@crud/RR.operation'
import { all as productAll } from '@/api/wms/stock'

const defaultForm = {
  billType: 1,
  defaultNumber: null,
  depotId: null,
  inProductId: null,
  adjQuantityBefore: null,
  adjQuantity: null,
  adjQuantityAfter: null,
  ratio: 0,
  remark: null,
  sourceId: null,
  sourceNo: null
}
export default {
  name: 'Adjustment',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '庫存調整單',
      url: 'transferBill/page',
      sort: 'id:desc',
      crudMethod: { ...crudAdjustment },
      query: { billType: 1 },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: false
      }
    })
  },
  data() {
    return {
      depot: [],
      depotBillIn: [],
      productQuery: { page: 1, size: 10 },
      BillInProductVisible: false,
      inProduct: null,
      viewDetailVisible: false,
      seeForm: {},
      selectionDepotBillInProduct: [],
      permission: {
        add: ['admin', 'stockBill:add'],
        edit: ['admin', 'stockBill:edit'],
        del: ['admin', 'stockBill:del']
      },
      rules: {
        defaultNumber: [
          { required: true, message: '单据号不能为空', trigger: 'blur' }
        ],
        depot: [
          { required: true, message: '仓库不能为空', trigger: 'blur' }
        ],
        adjQuantity: [
          { required: true, type: 'number', message: '請輸入數字', trigger: 'change' }
        ],
        sourceNo: [
          { required: true, message: '入庫單号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToCU]() {
      this.getDepot()
      this.getDepotBillIn()
    },
    [CRUD.HOOK.afterToCU]() {
      this.inProduct = null
    },
    [CRUD.HOOK.afterAddCancel]() {
      this.inProduct = null
    },
    getDepot() {
      getAll().then(res => {
        this.depot = res.data
      })
    },
    getDepotBillIn() {
      depotBillIn.billInItemPage({ depotId: this.form.depotId, ...this.productQuery }).then(res => {
        this.depotBillIn = res.data.rows
        this.productQuery.total = res.data.total
      })
    },
    showBillInDialog() {
      this.BillInProductVisible = true
    },
    getSelectionDepotBillIn(sel) {
      this.selectionDepotBillInProduct = sel
    },
    BillInProduct() {
      this.BillInProductVisible = false
      this.inProduct = this.selectionDepotBillInProduct[0]
      this.form.inProductId = this.inProduct.product.id
      this.form.sourceNo = this.inProduct.depotBillInCode
      this.form.sourceId = this.inProduct.depotBillInId
      this.$refs.BillInTable.clearSelection()
      productAll({ depotId: this.form.depotId, productId: this.form.inProductId }).then(res => {
        if (res.code === 0) {
          this.form.adjQuantityBefore = res.data[0].quantity
        }
      })
    },
    seeTransfer(sel) {
      this.seeForm = sel
      this.viewDetailVisible = true
    },
    toQuery() {
      this.getDepotBillIn()
    },
    // 当前页改变
    pageChangeHandler(e) {
      this.productQuery.page = e
      this.getDepotBillIn()
    },
    // 每页条数改变
    sizeChangeHandler(e) {
      this.productQuery.size = e
      this.productQuery.page = 1
      this.getDepotBillIn()
    },
    computeAfter(e) {
      console.log(e)
      this.form.adjQuantityAfter = this.form.adjQuantityBefore + this.form.adjQuantity
    }
  }
}
</script>

<style scoped>

</style>
